package com.corejsf;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
/**
 *
 * Scheduler class is used by each user to generate a unique current timesheet,
 * and keeps track of the timesheet's history.
 *
 * @author Victor Lu
 * @version 1.0
 *
 */
public class Scheduler implements Serializable {
    /**
     *  serialVersionUID.
     */
    private static final long serialVersionUID = 2269996014365436200L;
    /**
     * timeSheet - Arraylist of Timesheet beans (which are the displayed rows).
     */
    private ArrayList<Timesheet> timeSheet;
    /**
     * oldTimeSheets - Arraylist of Arraylists of Timesheets. Entire Table of a
     * Timesheet is stored to an old timesheet to store into the history.
     */
    private ArrayList<ArrayList<Timesheet>> oldTimeSheets
        = new ArrayList<ArrayList<Timesheet>>();
    /**
     * Initial week number that keeps stays the same.
     */
    private int oldWeekNum = 1;
    /**
     * Intitial week number.
     */
    private int weekNum = 1;
    /**
     * LinkedHashMap holds the values of the dropdown list of weeks.
     */
    private LinkedHashMap<String, Integer> weekCount;

    /**
     * Constructor.
     */
    public Scheduler() {
    }

    /**
     * In case there are no values, the timesheet is initialized with
     * 5 empty rows.
     * @return a set of timeSheets.
     */
    public final ArrayList<Timesheet> getTimeSheet() {
        if (timeSheet == null) {
            timeSheet = new ArrayList<Timesheet>();
            for (int i = 0; i < 5; i++) {
                timeSheet.add(new Timesheet());
            }
        }
        return timeSheet;
    }

    /**
     * Sets the current timesheet.
     * @param timeSheet a timesheet for that week.
     */
    public final void setTimeSheet(final ArrayList<Timesheet> timeSheet) {
        this.timeSheet = timeSheet;
    }

    /**
     * Grabs the old timesheets that have been previously submitted.
     *
     * @return all timesheets
     */
    public final ArrayList<ArrayList<Timesheet>> getOldTimeSheets() {
        return oldTimeSheets;
    }

    /**
     * Pushes the timesheet into the oldTimeSheets arrayList.
     * Generates 5 new timeSheets to display
     * for the week.
     * Returns the editSchedulePage once again.
     * @return navigation outcome string
     */
    public final String nextWeek() {
        getOldTimeSheets().add(getTimeSheet());
        weekNum++;
        timeSheet = null;
        getTimeSheet();
        return "editSchedule";
    }

    /**
     * Adds a row to the current timeSheet.
     */
    public final void addRow() {
        if (timeSheet.size() < 10) {
            timeSheet.add(new Timesheet());
        }
    }

    /**
     * Deletes a row from the current timeSheet.
     */
    public final void deleteRow() {
        if (timeSheet.size() != 0) {
            timeSheet.remove(timeSheet.size() - 1);
        }
    }

    /**
     * Sums up the 'Total' columnn.
     *
     * @return the total of the column.
     */
    public final double getColumnTotal() {
        double columnTotal = 0;
        for (int i = 0; i < timeSheet.size(); i++) {
            columnTotal += timeSheet.get(i).getTotal();
        }
        return columnTotal;
    }

    /**
     * Sums up the 'Sat' column.
     *
     * @return the total of the column.
     */
    public final double getSatTotal() {
        double satTotal = 0;
        for (int i = 0; i < timeSheet.size(); i++) {
            satTotal += timeSheet.get(i).getSat();
        }
        return satTotal;
    }

    /**
     * Sums up the 'Sun' column.
     *
     * @return the total of the column.
     */
    public final double getSunTotal() {
        double sunTotal = 0;
        for (int i = 0; i < timeSheet.size(); i++) {
            sunTotal += timeSheet.get(i).getSun();
        }
        return sunTotal;
    }

    /**
     * Sums up the 'Mon' column.
     *
     * @return the total of the column.
     */
    public final double getMonTotal() {
        double monTotal = 0;
        for (int i = 0; i < timeSheet.size(); i++) {
            monTotal += timeSheet.get(i).getMon();
        }
        return monTotal;
    }

    /**
     * Sums the 'Tue' column.
     *
     * @return the total of the column.
     */
    public final double getTueTotal() {
        double tueTotal = 0;
        for (int i = 0; i < timeSheet.size(); i++) {
            tueTotal += timeSheet.get(i).getTue();
        }
        return tueTotal;
    }

    /**
     * Sums up the 'Wed' column.
     *
     * @return the total of the column.
     */
    public final double getWedTotal() {
        double wedTotal = 0;
        for (int i = 0; i < timeSheet.size(); i++) {
            wedTotal += timeSheet.get(i).getWed();
        }
        return wedTotal;
    }

    /**
     * Sums up the 'Thu' column.
     *
     * @return the total of the column.
     */
    public final double getThuTotal() {
        double thuTotal = 0;
        for (int i = 0; i < timeSheet.size(); i++) {
            thuTotal += timeSheet.get(i).getThu();
        }
        return thuTotal;
    }

    /**
     * Sums up the 'Fri' column.
     *
     *@return the total of the column.
     */
    public final double getFriTotal() {
        double friTotal = 0;
        for (int i = 0; i < timeSheet.size(); i++) {
            friTotal += timeSheet.get(i).getFri();
        }
        return friTotal;
    }

    /**
     * Gets the current week number.
     *
     * @return weekNum.
     */
    public final int getWeekNum() {
        return weekNum;
    }

    /**
     * Sets the current week number.
     *
     * @param weekNum current week number
     */
    public final void setWeekNum(final int weekNum) {
        this.weekNum = weekNum;
    }

    /**
     * Adds the current week into the dropdown list represented
     * by a linked hash map.
     *
     * @return week count.
     */
    public final LinkedHashMap<String, Integer> getWeekCount() {
        int weeks = getWeekNum() - 1;
        weekCount = new LinkedHashMap<String, Integer>();
        for (int i = 1; i <= weeks; i++) {
            this.weekCount.put("Week " + i, i);
        }
        return weekCount;
    }

    /**
     * Gets the old week number.
     *
     * @return oldWeekNum
     */
    public final int getOldWeekNum() {
        return oldWeekNum;
    }

    /**
     * Sets the old week number.
     * @param oldWeekNum old week number
     */
    public final void setOldWeekNum(final int oldWeekNum) {
        this.oldWeekNum = oldWeekNum;
    }

    /**
     * Get the values of the old week.
     *
     * This function only called if oldTimeSheets.size() != 0
     * see oldTimeSheets.xhtml
     *
     * @return the stored TimeSheets
     */
    public final ArrayList<Timesheet> getOldWeek() {
        return oldTimeSheets.get(oldWeekNum - 1);
    }
}
